// TODO: CODE REVIEW & CLEANUP!
//
// AlertsScreen.xaml.cs
//

using System;
using System.Windows;
using System.Windows.Controls;
using HomeUX.Controls;
using HomeUX.Utilities;

namespace HomeUX.ClientAppSupport
{
public partial class AlertsScreen : UserControl, IAlertsDisplay
{
    // the max. no. alerts to display -- older items are truncated as needed
    const int MaxItems = 200;

    Screen _screen;

    //Brush _newAlertTabBackgroundBrush;

    // the index number displayed for the last-shown alert
    int _lastAlertNumber;

    public AlertsScreen()
    {
        InitializeComponent();
        Loaded += AlertsScreen_Loaded;
        //_alertMessageStyle = (Style)App.Current.Resources["AlertMessageStyle"];
        //_newAlertTabBackgroundBrush = (Brush)App.Current.Resources["NewAlertTabBackground"];
    }

    void AlertsScreen_Loaded(object sender, RoutedEventArgs e)
    {
        _screen = ClientFramework.Current.TryFindScreen(this);
        _screen.Opened += _screen_Opened;
        _screen.ShowModalMessageBox("There are no messages to display");
    }

    //TouchMessageBox _noAlertsMessage;

    void _screen_Opened(object sender, EventArgs e)
    {
        ShowIconInTab(0);
        //if (MessageList.Children.Count == 0)
        //{
        //    _noAlertsMessage = TouchMessageBox.Show(this, "There are no alerts to display", null,
        //        delegate
        //        {
        //            _noAlertsMessage = null;
        //        });
        //}
    }

    //void HighlightTab(bool highlight)
    //{
    //    var border = (Border)_screen.TabItem.FindName("AlertsTabBorder");
    //    border.Background = highlight ? _newAlertTabBackgroundBrush : null;
    //}

    void ShowIconInTab(AlertType alertType)
    {
        var informationIcon = (InformationIcon)_screen.TabItem.FindName("AlertsTabInformationIcon");
        var warningIcon = (WarningIcon)_screen.TabItem.FindName("AlertsTabWarningIcon");

        informationIcon.Visibility =
            ((alertType == AlertType.Information) ? Visibility.Visible : Visibility.Collapsed);
        warningIcon.Visibility =
            ((alertType == AlertType.Warning) ? Visibility.Visible : Visibility.Collapsed);

        _displayedAlertType = alertType;
    }

    AlertType _displayedAlertType;

    void IAlertsDisplay.AddMessage(AlertType alertType, string message)
    {
        if (alertType > _displayedAlertType)
            ShowIconInTab(alertType);

        // if the "no alerts to display" message is being displayed (i.e. the user was on the
        // Alerts screen when this method was called), hide the message
        //if (_noAlertsMessage != null)
        //    ControlHelper.CloseDialog(_noAlertsMessage, false);
        _screen.CloseModalMessageBox();

        var clientStatus = ClientFramework.Current.ClientStatus;
        message = String.Format("[{0}] {1} {2}: {3}", ++_lastAlertNumber,
            clientStatus.CurrentDateString, clientStatus.CurrentTimeString, message);

        var logItem = new LogItem()
        {
            Message = message
        };

        switch (alertType)
        {
        case AlertType.Information:
            logItem.Icon = new InformationIcon();
            break;
        case AlertType.Warning:
            logItem.Icon = new WarningIcon();
            break;
        }

        UIElementCollection items = MessageList.Children;

        items.Add(logItem);

        while (items.Count > MaxItems)
            items.RemoveAt(0);

        Dispatcher.BeginInvoke(delegate
        {
            MessageScrollViewer.ScrollIntoView(logItem, ScrollAlignment.Bottom);
        });
    }
}

}
